package com.ja.inquiry.service.impl;

import com.ja.inquiry.mapper.SupplierMapper;
import com.ja.inquiry.pojo.PlanClassSelectVo;
import com.ja.inquiry.pojo.Supplier;
import com.ja.inquiry.pojo.SupplierSelectVo;
import com.ja.inquiry.service.SupplierService;
import com.ja.util.AllRecords;
import com.ja.util.ResultBuildVo;
import com.ja.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * Created by 11936 on 2019/4/28.
 */

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public ResultVo queryByLegalPerson(Integer legalPersonId) {
        return ResultBuildVo.success( supplierMapper.queryByLegalPerson(legalPersonId));
    }

    @Override
    public ResultVo searchPartnerPage(SupplierSelectVo supplierSelectVo) {
        int count = supplierMapper.querySupplierCount(supplierSelectVo);
        List<Supplier> list = supplierMapper.selectSupplier(supplierSelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setPageSize(supplierSelectVo.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(list);
        return ResultBuildVo.success(allRecords);
    }

    @Override
    public ResultVo queryByIdExamine(String requestForQuotationId) {
        return ResultBuildVo.success(supplierMapper.queryByIdExamine(requestForQuotationId));
    }

    @Override
    public ResultVo queryPartner(Integer userid) {
        return ResultBuildVo.success(supplierMapper.queryByIdPerson(userid));
    }

    @Override
    public ResultVo queryByForQuotationId(String requestForQuotationId) {
        supplierMapper.queryByForQuotationId(requestForQuotationId);
        return null;
    }

    @Override
    //导出
    //1.定义一个导出模板
    //2.从数据库中查询出将要导出的数据
    //3.把从数据库中查询出的数据赋值给导出模板
    //4.对需要转化的数据进行转化
    //5.在浏览器生成一个文件
    public ResultVo exportExcel(HttpServletResponse resp, String code, String name, Integer count, String currency) {

        PlanClassSelectVo planClassSelectVo = new PlanClassSelectVo();
        planClassSelectVo.setCode(code);
        planClassSelectVo.setCount(count);
        planClassSelectVo.setCurrency(currency);
        planClassSelectVo.setName(name);
        List<PlanClassSelectVo> planClassVos = supplierMapper.queryPlanClass(planClassSelectVo);
        //给数据中的中文赋值
//        assigment(planClassVos);
        //获取响应输出流
        ServletOutputStream out = response.getOutputStream();
        //给输出文件设置名称
        POIClass.toPackageOs(response, "询价模板");
        //读取模板中的数据
        InputStream in = ExportUtil.toPackageIn("static/备件小类.xlsx");
        //根据模板的数据、把查询出来的数据给摸版SHeet1组中的数据赋值、把excel输出到浏览器上
        writeDataToExcel(in, "Sheet1", planClassVos, out);
        if (in != null) {
            in.close();
            out.close();
        }


        return ResultBuildVo.success(null);
    }
}
